#include "playscene.h"
#include <QDebug>
#include <QMenuBar>
#include <QLabel>
#include <QPainter>
#include <QPixmap>
#include "mypushbutton.h"
#include "mycoin.h"
#include "dataconfig.h"
#include <QTimer>
#include <QPropertyAnimation>
#include <QSound>
//PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
//{

//}
PlayScene::PlayScene(int level)
{
    qDebug()<<QString("进入了第%1关").arg(level);
    this->levelIndex = level;

    //初始化游戏场景
    this->setFixedSize(390,590);

    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));

    this->setWindowTitle("选择场景关卡");

    QSound *flipSound = new QSound(":/res/ConFlipSound.wav",this);


    //创建该界面的菜单栏
    QMenuBar *bar = menuBar();
    setMenuBar(bar);

    //创建开始菜单
    QMenu *startMenu = bar->addMenu("菜单");

    //创建退出菜单项
    QAction *quitaction = startMenu->addAction("退出");
    QAction *treataction = startMenu->addAction("开挂");

    connect(quitaction,&QAction::triggered,[=]{
        this->close();
    });

    connect(treataction,&QAction::triggered,[=]{
            for(int i=0;i<16;i++)
                if(coinBtn[i/4][i%4]->flag==false)
                    coinBtn[i/4][i%4]->changeFlag();

            this->has_winned();
    });

    //胜利图片显示
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setParent(this);
    winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->move( (this->width()-tmpPix.width()) * 0.5, -tmpPix.height());

    myPushButton *backBtn = new myPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    //点击返回
    connect(backBtn,&QPushButton::clicked,[=]{
        qDebug()<<"在游戏场景点击了返回按钮";
        emit this->PlayScencBack();
    });



    //显示当前的关卡数
    QLabel *label = new QLabel(this);
    label->setFixedSize(140,40);

    label->move(30,540);
    QFont *font = new QFont();
    font->setFamily("华文新魏");
    font->setPointSize(25);
    label->setFont(*font);
    label->setText(QString("level-%1").arg(level));

    QPixmap pix;
    bool ret = pix.load(":/res/BoardNode(1).png");
    if(!ret) qDebug()<<pix<<"图片加载失败";

    dataConfig array;
    //显示金币银币
    for(int i=0; i<16; i++)
    {
        QLabel *label = new QLabel(this);
        label->setFixedSize(pix.width(),pix.height());

        label->setPixmap(pix);
        label->move(90+i/4*50,200+i%4*50);
        MyCoin *coin;
        if(array.mData[this->levelIndex][i/4][i%4])
        {
            coin = new MyCoin(":/res/Coin0001.png");
            coin->flag = true;
        }
        else
        {
            coin = new MyCoin(":/res/Coin0008.png");
            coin->flag = false;
        }
        coin->setParent(this);
        coin->move(92+i/4*50,202+i%4*50);
        //记录每个币的坐标
        coin->PosX = i/4;
        coin->PosY = i%4;


        //将金币放入到金币的二维数组里面 以便于后期的维护
        coinBtn[i/4][i%4] = coin;

        connect(coin,&QPushButton::clicked,[=]{

            flipSound->play();

            //按下金币按钮时禁止其他金币按钮按键
            for(int i=0;i<16;i++)
                coinBtn[i/4][i%4]->is_Win = true;

            coin->changeFlag();


            QTimer::singleShot(300,this,[=](){

                //检测左侧币是否可翻转
                if(coin->PosX-1 >= 0) coinBtn[coin->PosX-1][coin->PosY]->changeFlag();

                //检测右侧币是否可翻转
                if(coin->PosX+1 <= 3) coinBtn[coin->PosX+1][coin->PosY]->changeFlag();

                //检测上侧币是否可翻转
                if(coin->PosY-1 >= 0) coinBtn[coin->PosX][coin->PosY-1]->changeFlag();

                //检测下侧币是否可翻转
                if(coin->PosY+1 <= 3) coinBtn[coin->PosX][coin->PosY+1]->changeFlag();

                //按下金币按钮时解除禁止其他金币按钮按键
                for(int i=0;i<16;i++)
                    coinBtn[i/4][i%4]->is_Win = false;

                this->has_winned();


            });
        });
    }
}


void PlayScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);


    //加载图片
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载标题
    pix.load(":/res/Title.png");
    //缩放
    painter.drawPixmap((this->width()-pix.width())-0.5, 30, pix.width(), pix.height(), pix);

    bool ret = pix.load(":/res/BoardNode(1).png");
    if(!ret)
    {
        qDebug()<<"图片加载失败";
    }
    painter.drawPixmap(30,540,140,40,pix);

}
void PlayScene::has_winned(void)
{
    QSound *winSound = new QSound(":/res/LevelWinSound.wav",this);
    //判断是否胜利
    this->is_Win=true;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            if(coinBtn[i][j]->flag==false)
            {
                this->is_Win=false;
                break;
            }
        }
    }

    if(this->is_Win)
    {
        winSound->play();
        qDebug()<<"winner winner";
        for(int i=0;i<4;i++)
            for(int j=0;j<4;j++)
                coinBtn[i][j]->is_Win=true;

        //将胜利的图片移动下来
        QPropertyAnimation *animation = new QPropertyAnimation(winLabel,"geometry");
        //设置时间间隔
        animation->setDuration(1000);
        //设置开始位置
        animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
        //设置结束位置
        animation->setEndValue(QRect(winLabel->x(),winLabel->y()+150,winLabel->width(),winLabel->height()));
        //设置缓和曲线
        animation->setEasingCurve(QEasingCurve::OutBounce);
        //执行动画
        animation->start();
    }
}
